/**
 * 
 */
package com.easidea.tim.service.finance;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springside.core.service.EntityService;

import com.easidea.tim.dao.finance.AccountAgeReportDao;
import com.easidea.tim.entity.finance.AccountAgeReport;

/**
 * @author Harrison
 *
 */
public class AccountAgeReportService extends EntityService<AccountAgeReport, AccountAgeReportDao> {

	private AccountAgeReportDao accountAgeReportDao;
	private Logger log = Logger.getLogger(AccountAgeReportService.class);
	
	/**
	 * 
	 * @param customerName
	 * @param staffName
	 * @return
	 */
	public List<AccountAgeReport> queryList(String customerName, String staffName){
		if(accountAgeReportDao != null){
			//调用数据生成
			genReportCurrently(); 
			
			//数据提取
			Criteria cte = accountAgeReportDao.createUnDeletedCriteria();
			if(StringUtils.isNotEmpty(customerName)){
				cte.add(Restrictions.eq("custName", customerName));
			}
			
			if(StringUtils.isNotBlank(staffName)){
				cte.add(Restrictions.eq("staffName", staffName));
			}
			cte.addOrder(Order.desc("custAmountSum")); //根据客户总余额降序排列
			List<AccountAgeReport> list = cte.list();
			return list;
		}else
			return null;
	}
	
	/**
	 * 即时生成数据
	 */
	private void genReportCurrently(){
		accountAgeReportDao.getHibernateTemplate().execute(new HibernateCallback() { 
            public Object doInHibernate(Session session) { 
                try { 
                	log.info("start customer account age report generation ~!");
                	long t = System.currentTimeMillis();
                    Connection conn = session.connection(); 
                    String sql = "{call sp_genFinanceAccountAgeReport()}"; 
                    CallableStatement stmt = conn.prepareCall(sql); 
                    stmt.execute(); 
                    log.info("completed account age report process, elapsed : " + (System.currentTimeMillis() - t)/1000 + " seconds ~~!");
                } catch (Exception e) { 
                	log.error("process customer account age report failure : "+e.getMessage(), e);
                }
                return null; 
            } 
        });
	}
	
	/* (non-Javadoc)
	 * @see org.springside.core.service.EntityService#getEntityDao()
	 */
	@Override
	public AccountAgeReportDao getEntityDao() {
		return accountAgeReportDao;
	}

	/**
	 * @return the accountAgeReportDao
	 */
	public AccountAgeReportDao getAccountAgeReportDao() {
		return accountAgeReportDao;
	}

	/**
	 * @param accountAgeReportDao the accountAgeReportDao to set
	 */
	public void setAccountAgeReportDao(AccountAgeReportDao accountAgeReportDao) {
		this.accountAgeReportDao = accountAgeReportDao;
	}

}
